import * as types from "./mutation-types";
import Api from "api/api";
const state = {
  banners: [],
  currentBanner: {
    articleUrl: "",
    fileId: null,
    fileURL: "",
    sort: 0,
    canClick: true
  },
  adminInfo: {},
  areaTree: [],
  memberIntro: {
    content: ""
  },
  uploadToken: "",
  refresh: false
};

const getters = {
  banners: state => state.banners,
  currentBanner: state => state.currentBanner,
  adminInfo: state => state.adminInfo,
  memberIntro: state => state.memberIntro,
  areaTree: state => state.areaTree,
  uploadToken: state => state.uploadToken
};

const actions = {
  getUploadFileToken({ commit }, key) {
    return Api.getUploadFileToken({ fileName: key }).then(ret => {
      return ret;
    });
  },
  login({ commit }, user) {
    return Api.login(user).then(ret => {
      console.log(ret);
      window.sessionStorage.setItem("token", ret.data.data);
      return ret;
    });
  },
  updatePassword({ commit }, payload) {
    return Api.updateAuthUserPassword(payload).then(ret => {
      return ret;
    });
  },
  getAdminInfo({ commit }) {
    return Api.getTSUser().then(ret => {
      commit(types.UPDATE_ADMIN_INFO, ret.data.data);
      window.sessionStorage.setItem("roleKeys", ret.data.data.userPermissions);
      return ret;
    });
  },
  // getAdmins({ commit }) {
  //   return Api.getAuthUsers().then(ret => {
  //     commit(types.UPDATE_ADMINS, ret.body);
  //     return ret;
  //   });
  // },
  // addAdmin({ commit }, param) {
  //   return Api.addUser(param).then(ret => {
  //     return ret;
  //   });
  // },
  // deleteAdmin({ commit }, param) {
  //   return Api.deleteUser(param).then(ret => {
  //     return ret;
  //   });
  // },
  // resetAdminPassword({ commit }, param) {
  //   return Api.resetUserPassword(param).then(ret => {
  //     return ret;
  //   });
  // },
  // changeAdminName({ commit }, param) {
  //   return Api.updateUserName(param).then(ret => {
  //     return ret;
  //   });
  // },
  getBanners({ commit }, param) {
    return Api.getSlidePage(param).then(ret => {
      commit(types.UPDATE_BANNERS, ret.data.data);
      return ret;
    });
  },
  addBanner({ commit }, payload) {
    return Api.addOrUpdateSlide({ body: payload }).then(ret => {
      return ret;
    });
  },
  editBanner({ commit }, payload) {
    return Api.addOrUpdateSlide({ body: payload }).then(ret => {
      return ret;
    });
  },
  deleteSysHomeBanner({ commit }, id) {
    return Api.deleteSlide({ id: id }).then(ret => {
      return ret;
    });
  },
  saveMemberIntro({ commit }, payload) {
    return Api.updateMemberIntro({ body: payload }).then(ret => {
      return ret;
    });
  },
  getMemberIntro({ commit }) {
    return Api.getMemberIntro().then(ret => {
      commit(types.UPDATE_MEMBER_INTRO, ret.data.data);
      return ret;
    });
  },
  getAreasS({ state, commit }, param = {}) {
    return Api.getAreas(param).then(ret => {
      commit(types.UPDATE_AREA_OPTION, ret.data.data);
      return ret;
    });
  }
  // authPage({commit}, param) {
  //   return Api.authPage(param).then(ret => {
  //     return ret
  //   })
  // },
  // authSuccess({commit}, param) {
  //   return Api.authSuccess(param).then(ret => {
  //     return ret
  //   })
  // },
  // getWechatAccount({commit}, param) {
  //   return Api.getWechatAccount(param).then(ret => {
  //     switch (param.type) {
  //       case 'WXA':
  //         commit(types.UPDATE_WXA_INFO, ret.body)
  //         break
  //       case 'Official_Account':
  //         commit(types.UPDATE_OFFICIAL_ACCOUNT_INFO, ret.body)
  //         break
  //     }
  //     return ret
  //   })
  // },
  // cancelAuth({commit}, param) {
  //   return Api.cancelAuth(param).then(ret => {
  //     return ret
  //   })
  // },
  // getWechatPaySetting({commit}, payload) {
  //   return Api.getShopWechatPaySetting(payload).then(ret => {
  //     commit(types.UPDATE_PAY_SETTING, ret.body)
  //     return ret
  //   })
  // },
  // saveWechatPaySetting({commit}, payload) {
  //   return Api.addOrUpdateWechatPaySetting({body: payload}).then(ret => {
  //     return ret
  //   })
  // },
  // getHomeSetting({commit}, payload) {
  //   return Api.getHomeSetting(payload).then(ret => {
  //     commit(types.UPDATE_HOME_SETTING, ret.body)
  //     return ret
  //   })
  // },
  // saveHomeSetting({commit}, payload) {
  //   console.log('保存HomeSetting', payload)
  //   return Api.updateHomeSetting({body: payload}).then(ret => {
  //     return ret
  //   })
  // },
  // deleteFile({commit}, filename) {
  //   return Api.deleteFile({filename: filename}).then(ret => {
  //     return ret
  //   })
  // },
  // getMessageTpl({commit}, shopId) {
  //   return Api.accountGetMessageTemplate({shopId: shopId}).then(ret => {
  //     return ret
  //   })
  // },
  // bindOpenAccount({commit}, param) {
  //   return Api.bindOpenAccount(param).then(ret => {
  //     return ret
  //   })
  // },
  // commitCode({commit}, param) {
  //   return Api.commitCode(param).then(ret => {
  //     return ret
  //   })
  // },
  // getWxaCategories({commit}, param) {
  //   return Api.getWxaCategories(param).then(ret => {
  //     commit(types.UPDATE_WXA_CATEGORIES, ret.body)
  //     return ret
  //   })
  // },
  // submitAudit({commit}, payload) {
  //   return Api.submitAudit({body: payload}).then(ret => {
  //     return ret
  //   })
  // },
  // undoAudit({commit}, param) {
  //   return Api.undoAudit(param).then(ret => {
  //     return ret
  //   })
  // },
  // bindTestUser({commit}, param) {
  //   return Api.bindTestUser(param).then(ret => {
  //     return ret
  //   })
  // },
  // wxaRelease({commit}, appId) {
  //   return Api.wxaRelease({appId: appId}).then(ret => {
  //     return ret
  //   })
  // },
  // getTestQRCode({commit}, appId) {
  //   return Api.getTestQRCode({appId: appId}).then(ret => {
  //     return ret
  //   })
  // },
  // getProducts ({commit}) {
  //   return Api.getProducts().then(ret => {
  //     commit(types.UPDATE_PRODUCTS, ret.body)
  //     return ret
  //   })
  // }
};

const mutations = {
  [types.INIT_CURRENT_ADMIN](state) {
    state.currentAdmin = {
      username: "",
      name: "",
      password: ""
    };
  },
  [types.UPDATE_ADMINS](state, payload) {
    state.admins = payload;
  },
  [types.INIT_CURRENT_BANNER](state) {
    state.currentBanner = {
      articleUrl: "",
      fileId: null,
      fileURL: "",
      sort: 0,
      canClick: true
    };
  },
  [types.UPDATE_CURRENT_BANNER](state, payload) {
    state.currentBanner = payload;
  },
  [types.UPDATE_BANNERS](state, payload) {
    state.banners = payload;
  },
  [types.UPDATE_ADMIN_INFO](state, payload) {
    state.adminInfo = payload;
  },
  [types.UPDATE_MEMBER_INTRO](state, payload) {
    state.memberIntro = payload;
  },
  [types.UPDATE_AREA_OPTION](state, payload) {
    state.areaTree = payload;
  }
  // [types.UPDATE_FLATTEN_AREA](state) {
  //   if (state.flattenArea.length === 0) {
  //     _.forEach(state.areaOption, (province) => {
  //       state.flattenArea.push({code: province.code, name: province.name})
  //       _.forEach(province.children, (city) => {
  //         state.flattenArea.push({code: city.code, name: city.name})
  //         _.forEach(city.children, (area) => {
  //           state.flattenArea.push({code: area.code, name: area.name})
  //         })
  //       })
  //     })
  //   }
  // },
  // [types.UPDATE_WXA_INFO](state, payload) {
  //   const wxaInfo = {
  //     accountName: '',
  //     appId: '',
  //     testQRImg: '',
  //     testUsers: []
  //   }
  //   state.wxaInfo = _.merge(wxaInfo, payload)
  // },
  // [types.UPDATE_OFFICIAL_ACCOUNT_INFO](state, payload) {
  //   state.officialAccountInfo = payload
  // },
  // [types.UPDATE_PAY_SETTING](state, payload) {
  //   state.paySetting = _.merge(state.paySetting, payload)
  // },
  // [types.UPDATE_HOME_SETTING](state, payload) {
  //   state.homeSetting = _.merge(state.homeSetting, payload)
  // },
  // [types.UPDATE_WXA_CATEGORIES](state, payload) {
  //   state.wxaCategories = payload
  // },
  // [types.UPDATE_PRODUCTS] (state, payload) {
  //   state.products = payload
  // }
};

export default {
  state,
  getters,
  actions,
  mutations
};
